home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 23 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.0 KB  |  109 lines

  1. Path: news.umbc.edu!not-for-mail
  2. From: schlein@umbc.edu (Jonas J. Schlein)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: What's so different about 2-D arrays???
  5. Date: 31 Dec 1995 19:18:51 -0500
  6. Organization: University of Maryland Baltimore County
  7. Message-ID: <4c799b$q9d@umbc9.umbc.edu>
  8. References: <820451938.20697@fredblog.demon.co.uk>
  9. NNTP-Posting-Host: f-umbc9.umbc.edu
  10. NNTP-Posting-User: schlein
  11.  
  12. Mark Winfield <mark@fredblog.demon.co.uk> wrote:
  13. |> I have been teaching myself 'C' over the last month.  Just before Xmas
  14. |> I thought that I had it worked out and that all I had to do was
  15. |> practice.  I decided to write a program that would record chess games,
  16. |> it would allow move branching as well.  I decided upon this because it
  17. |> should use everything I have learnt except DOS calls.
  18. |> Now for the problem,
  19. |> 
  20. |> //My own chess recorder program
  21.  
  22. Illegal comment! This is comp.lang.c not comp.lang.c++.
  23.  
  24. |> #include <stdio.h>
  25. |> #include <ctype.h>
  26.  
  27. Why do you need <ctype.h>?
  28.  
  29. |> /*Prototypes*/
  30. |> void setup(char pos[8][8]);   
  31. |> 
  32. |> void main()
  33.  
  34. Please read the FAQ. This will show you why 'int main (void)' is the correct
  35. declaration for your particular program.
  36.  
  37. |> {
  38. |> char pos[8][8];
  39. |> 
  40. |>     setup(pos);
  41. |> 
  42. |> 
  43. |>     printf("\nWell Done!!");
  44.  
  45. A simple 'return (0);' would go here nicely.
  46.  
  47. |> }
  48. |> 
  49. |> void setup(char pos[8][8])
  50.  
  51. What exactly do you think an array is and how do you believe it is
  52. supposed to be indexed? In C arrays start at 0 so an array with
  53. 8 elements is indexed from 0-7 not 1-8. This is true for each and
  54. every dimension of a multi-dimensional array.
  55.  
  56. |> {
  57. |> int x,y;    
  58. |> 
  59. |>     pos[1][8]=pos[8][8]='r';
  60. |>     pos[2][8]=pos[7][8]='n';
  61. |>     pos[3][8]=pos[6][8]='b';
  62. |> 
  63. |>     pos[1][1]=pos[8][1]='R';
  64. |>     pos[2][1]=pos[7][1]='N';
  65. |>     pos[3][1]=pos[6][1]='B';
  66.  
  67. In each and every one of these 6 statements you explicitly access data
  68. beyond the array. Especially for pos[8][8] which is way outside your
  69. legal range!
  70.  
  71. |>     for(x=1;x<9;x++){
  72. |>         pos[x][7]='a';
  73. |>         pos[x][2]='A';
  74.  
  75. Here you go from 1-8 so when 'x == 8' you're in trouble.
  76.  
  77. |>         for(y=3;y<7;y++)
  78. |>             pos[x][y]=' ';
  79. |>     }
  80. |> 
  81. |>     pos[4][8]='q';
  82. |>     pos[5][8]='k';
  83.  
  84. More explicit accesses which are illegal.
  85.  
  86. |>     pos[4][1]='Q';
  87. |>     pos[5][1]='K';
  88. |> }
  89. |> 
  90. |> This program does not work with a '.c' or '.cpp' extension.  If I run
  91. |> the program from dos I get well done and then something about NULL
  92. |> pointers and the program hangs, so I have to reset. And if I run the
  93. |> program from within the TURBO C++ enviroment the program hangs after
  94. |> printing well done.
  95.  
  96. Are you saying you get problems about NULL pointers at run time or
  97. compile time?
  98.  
  99. |> I have written a similar program since, as a test, which uses a 1-D
  100. |> array with no problems.  Can someone help me with this problem.
  101.  
  102. Show us the 1D example for comparison...Maybe you just got lucky?
  103. However, it's always preferable to post the smallest ANSI C example
  104. which produces the problem which you have come close to doing.
  105. -- 
  106. "If it wasn't for C, we would be using BASI, PASAL, and OBOL."
  107.  
  108. Jonas J. Schlein  (schlein@gl.umbc.edu)
  109.